//
//
//

#include "xlispp.h"

extern "C" {
#include <xlisp.h>
};

//
// C++ adapter for xlisp
//

xlispqt::XlispEngine::XlispEngine(int argc, char **argv, CbProvider const *cb_provider)
{
	m_callbacks = xlDefaultCallbacks(argv[0]);
	if(cb_provider)
		cb_provider->setupCallbacks(this);
	xlInit(m_callbacks, 0, 0, NULL);
	xlInfo("%s", xlBanner());
	xlLoadFile("xlispqt.lsp");
}

xlispqt::XlispEngine::~XlispEngine(void)
{
}

//
// implement (eval (read (make-string-input-stream <text>)))
//
int
xlispqt::XlispEngine::eval(const char *text)
{
	int valc;
	xlValue val;

	valc = xlCallFunctionByName(&val, 1, "MAKE-STRING-INPUT-STREAM", 1, xlMakeCString(text));
	if (valc < 0)
		return xlsError;
	valc = xlCallFunctionByName(&val, 1, "READ", 1, val);
	if (valc < 0)
		return xlsError;
    valc = xlCallFunctionByName(&val, 1, "EVAL", 1, val);
	if (valc < 0)
		return xlsError;
	valc = xlCallFunctionByName(&val, 1, "PRINT", 1, val);
#if 0
	if (valc < 0)
		return xlsError;
	valc = xlCallFunctionByName(&val, 1, "DISPLAY", 1, xlMakeCString("\n> "));
#endif
    /* return status */
    return valc >= 0 && val == xlTrue ? xlsSuccess : xlsError;
}

void 
xlispqt::XlispEngine::setConsoleGetCCB(int (*consoleGetC)(void))
{
	this->m_callbacks->consoleGetC = consoleGetC;
}

void 
xlispqt::XlispEngine::setConsoleFlushInputCB(void (*consoleFlushInput)(void))
{
	this->m_callbacks->consoleFlushInput = consoleFlushInput;
}

void 
xlispqt::XlispEngine::setConsolePutCCB(void (*consolePutC)(int ch))
{
	this->m_callbacks->consolePutC = consolePutC;
}

void 
xlispqt::XlispEngine::setConsoleFlushOutputCB(void (*consoleFlushOutput)(void))
{
	this->m_callbacks->consoleFlushOutput = consoleFlushOutput;
}

void 
xlispqt::XlispEngine::setConsoleAtBOLPCB(int (*consoleAtBOLP)(void))
{
	this->m_callbacks->consoleAtBOLP = consoleAtBOLP;
}

void
xlispqt::XlispEngine::setConsoleCheckCB(int (*consoleCheck)(void))
{
	this->m_callbacks->consoleCheck = consoleCheck;
}

void 
xlispqt::XlispEngine::setExitCB(void (*exit)(int))
{
	this->m_callbacks->exit = exit;
}